package com.gome.ocean.service.datax.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.gome.ocean.common.exception.DatasourceServiceException;
import com.gome.ocean.dao.datax.DatasourceDAO;
import com.gome.ocean.dao.model.datasource.DatasourceBO;
import com.gome.ocean.service.datax.DatasourceService;

@Service
@Transactional("transactionManager")
public class DatasourceServiceImpl implements DatasourceService {

    private static final Logger LOG = LoggerFactory.getLogger(DatasourceServiceImpl.class);
    @Autowired
    private DatasourceDAO       datasourceDao;

    @Override
    public DatasourceBO findDatasourceBOById(Long id) {
        return datasourceDao.findDatasourceBOById(id);
    }

    @Override
    public List<DatasourceBO> findDataSourceByType(String type) {
        return datasourceDao.findDataSourceByType(type);
    }

    @Override
    public List<DatasourceBO> findDataSourceByTypeAndProjectNames(String type, List<String> projectNames)
                                                                                                         throws DatasourceServiceException {
        List<DatasourceBO> datasources;
        try {
            datasources = datasourceDao.findDataSourceByTypeAndProjectNames(type, projectNames);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new DatasourceServiceException(e.getMessage());
        }
        return datasources;
    }

    @Override
    public boolean insertDatasourceBO(DatasourceBO datasourceBO) throws DatasourceServiceException {
        return datasourceDao.insertDatasourceBO(datasourceBO) > 0;
    }

    @Override
    public boolean removeDatasourceBOById(Long id) throws DatasourceServiceException {
        return datasourceDao.removeDatasourceBOById(id) > 0;
    }

    @Override
    public boolean updateDatasourceBOById(DatasourceBO datasourceBO) throws DatasourceServiceException {
        return datasourceDao.updateDatasourceBOById(datasourceBO) > 0;
    }

    @Override
    public List<DatasourceBO> findAllDataSource() {
        return datasourceDao.findAllDataSource();
    }

    @Override
    public Page<DatasourceBO> findAllDatasourceBOsByProjectNames(List<String> projectNames, Integer pageNum,
                                                                 Integer pageSize) {
        return datasourceDao.findAllDatasourceBOsByProjectNames(projectNames, new RowBounds(pageNum, pageSize));
    }

    @Override
    public Page<DatasourceBO> findDataSources(Integer pageNum, Integer pageSize) throws DatasourceServiceException {
        Page<DatasourceBO> datasourceBOs;
        try {
            datasourceBOs = (Page<DatasourceBO>) datasourceDao.findDataSources(new RowBounds(pageNum, pageSize));
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new DatasourceServiceException(e.getMessage());
        }
        return datasourceBOs;
    }

    @Override
    public Page<DatasourceBO> findDataSourcesByUser(String createUser, Integer pageNum, Integer pageSize)
                                                                                                         throws DatasourceServiceException {
        Page<DatasourceBO> datasourceBOs;
        try {
            datasourceBOs = (Page<DatasourceBO>) datasourceDao.findDataSourcesByUser(createUser, new RowBounds(pageNum,
                pageSize));
        } catch (Exception e) {
            throw new DatasourceServiceException(e.getMessage());
        }
        return datasourceBOs;
    }

    @Override
    public List<DatasourceBO> findDataSourcesByUserAndType(String createUser, String type) {
        return datasourceDao.findDataSourcesByTypeAndUser(createUser, type);
    }

    @Override
    public List<DatasourceBO> findDataSourcesByProjectNamesAndType(List<String> projectNames, int type) {
        return datasourceDao.findDataSourcesByProjectNamesAndType(projectNames, type);
    }

    @Override
    public List<DatasourceBO> findDataSourcesByBeloneToCurrentUserProjects(List<String> projectNames) {
        try {
            List<DatasourceBO> list = datasourceDao.findDataSourcesByCurrentUserBelongToProjects(projectNames);
            if (list != null && list.size() > 0) {
                return list;
            }
        } catch (Exception e) {
            LOG.error(e.toString());
        }
        return null;
    }

	@Override
	public DatasourceBO findDatasourceBOByTypeAndUrl(String sourceType, String jdbcUrl) {
		return datasourceDao.findDatasourceBOByTypeAndUrl(sourceType, jdbcUrl);
	}
}
